﻿#region

using PicSizer.Program.Static;
using PicSizer.Program.Window.Partial;
using static PicSizer.Program.Static.PicUnit;

#endregion

namespace PicSizer.Program.Window.Forms
{
    public partial class SettingForm
    {
        /// <summary>
        /// 加载设置
        /// </summary>
        private void LoadSetting()
        {
            //压缩
            this.comboBox_CompressType.SelectedIndex = PicSetting.CompressType.ToInt();
            this.numericUpDown_Quality.Value = PicSetting.Quality; //指定画质

            long size = PicSetting.LimitSize;
            if (size > 1024)
            {
                size /= 1024;
                this.comboBox_KB_or_MB.SelectedIndex = 1; //MB
            }
            else
            {
                this.comboBox_KB_or_MB.SelectedIndex = 0; //KB
            }

            this.numericUpDown_Size.Value = size; //指定大小

            //尺寸
            this.comboBox_ResizeMode.SelectedIndex = PicSetting.ResizeType.ToInt(); //尺寸修正
            this.numericUpDown_LimitWidth.Value = PicSetting.LimitWidth;
            this.numericUpDown_LimitHeight.Value = PicSetting.LimitHeight;

            //命名
            this.comboBox_ExtensionMode.SelectedIndex = PicSetting.ExtensionType.ToInt(); //后缀格式
            this.numericUpDown_StartIndex.Value = PicSetting.OutputIndex; //起始下表
            this.textBox_OutputFilename.Text = PicSetting.OutputFilename; //自定名称

            //其它
            this.checkBox_AcceptExceedPicture.Checked = PicSetting.AcceptExceedPicture; //是否接受超出限制的文件
            this.checkBox_TopMost.Checked = PicSetting.TopMost; //置顶
            this.numericUpDown_Threads.Value = PicSetting.MaxThreads; //压缩线程数

            //图像处理
            this.trackBar_Brightness.Value = PicSetting.Brightness; //亮度
            this.checkBox_UseGPU.Checked = PicSetting.UseGPU; //硬件加速
            this.label_BackgroundColor.BackColor = Support.BytesToColor(PicSetting.BackgroundColor); //背景色

            //水印
            this.comboBox_WatermarkMode.SelectedIndex = PicSetting.WatermarkType.ToInt();
            this.label_WatermarkColor.BackColor = this.label_WatermarkFont.ForeColor
                = Support.BytesToColor(PicSetting.WatermarkColor); //水印颜色
            this.label_WatermarkFont.Font = PicSetting.WatermarkFont; //水印字体
            this.numericUpDown_WatermarkTransparency.Value = PicSetting.WatermarkAlpha; //水印不透明度
            this.textBox_WatermarkText.Text = PicSetting.WatermarkText; //水印文本

            //硬件加速
            if (PicValue.IsGpuSupport)
            {
                this.checkBox_UseGPU.Enabled = true;
                this.checkBox_UseGPU.Checked = PicSetting.UseGPU;
                string ToolTipStr = "new string(PicValue.picDllResult.GpuName).TrimEnd(Convert.ToChar(0))\n" +
                                    $"ThreadsPerBlock:{PicValue.PicDllResult.GpuMaxThreadsPerBlock}\n" +
                                    $"GridNum:{PicValue.PicDllResult.GpuGrid[0]},{PicValue.PicDllResult.GpuGrid[1]},{PicValue.PicDllResult.GpuGrid[2]}";
                this.toolTip_GPU.SetToolTip(this.checkBox_UseGPU, ToolTipStr);
            }
        }

        /// <summary>
        /// 保存设置
        /// </summary>
        private void SaveSetting()
        {
            //压缩
            PicSetting.CompressType = (CompressType)this.comboBox_CompressType.SelectedIndex; //JPEG压缩模式
            PicSetting.Quality = (int)this.numericUpDown_Quality.Value; //指定画质
            PicSetting.LimitSize = this.comboBox_KB_or_MB.SelectedIndex == 0
                ? (long)this.numericUpDown_Size.Value
                : (long)this.numericUpDown_Size.Value * 1024; //指定大小

            //尺寸
            PicSetting.ResizeType = (ResizeType)this.comboBox_ResizeMode.SelectedIndex; //尺寸修正
            PicSetting.LimitWidth = (int)this.numericUpDown_LimitWidth.Value;
            PicSetting.LimitHeight = (int)this.numericUpDown_LimitHeight.Value;

            //命名
            PicSetting.ExtensionType = (ExtensionType)this.comboBox_ExtensionMode.SelectedIndex; //后缀格式
            PicSetting.OutputIndex = (int)this.numericUpDown_StartIndex.Value; //起始下表
            PicSetting.OutputFilename = this.textBox_OutputFilename.Text; //自定名称

            //其它
            PicSetting.AcceptExceedPicture = this.checkBox_AcceptExceedPicture.Checked; //是否接受超出限制的文件
            PicSetting.TopMost = this.checkBox_TopMost.Checked; //置顶
            PicSetting.MaxThreads = (int)this.numericUpDown_Threads.Value;

            //图像处理
            PicSetting.Brightness = (byte)this.trackBar_Brightness.Value; //亮度
            PicSetting.BackgroundColor = Support.ColorToBytes(this.label_BackgroundColor.BackColor); //背景色

            //水印
            PicSetting.WatermarkType = (WatermarkType)this.comboBox_WatermarkMode.SelectedIndex;
            PicSetting.WatermarkColor = Support.ColorToBytes(this.label_WatermarkColor.BackColor); //水印颜色
            PicSetting.WatermarkFont = this.label_WatermarkFont.Font; //水印字体
            PicSetting.WatermarkAlpha = (byte)this.numericUpDown_WatermarkTransparency.Value; //水印不透明度
            PicSetting.WatermarkText = this.textBox_WatermarkText.Text; //水印文本

            //硬件加速
            PicSetting.UseGPU = PicValue.IsGpuSupport && this.checkBox_UseGPU.Checked;
        }
    }
}